﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Model;
using SDT.Lib.Common;
using SDT.Lib.Paging;
using SDT.Admin.ViewModels;
using System.Web.Security;
using SDT.Admin.Properties;
using SDT.Lib.Helpers;

namespace SDT.Admin.Controllers
{
    [RoleHelper(Roles = RoleSystem.Dictionary)]
    public class ShippingController : Controller
    {
        //
        // GET: /Shipping/

        SDTDataContext eDataContext = new SDTDataContext();
        public ActionResult Index(int? page)
        {
            try
            {
                if (!page.HasValue) page = 1;
                string keyword = UserState.getUserStateFromRequest("shipping.Keyword", "Keyword", "").ToString();
                int pageSize = int.Parse(UserState.getUserStateFromRequest("shipping.pageSize", "PageSize", 20).ToString());
                var ships = eDataContext.Shippings
                   .Where(p => (string.IsNullOrEmpty(keyword) ? true :
                                (p.ID.ToString().ToLower().Contains(keyword.ToLower().Trim())
                                ))).ToPagedList(page.Value - 1, pageSize);

                return View(new ShippingViewModels
                {
                    Shippings = ships,
                    Keyword = keyword
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region Create Price
        public ActionResult Create()
        {
            try
            {
                return View(
                    new ShippingEditViewModels
                    {
                        ProductCategories = eDataContext.ProductCategories.OrderBy(e => e.Name).ToList()
                    }
                );
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [HttpPost]
        public ActionResult Create(ShippingEditViewModels p)
        {
            try
            {
                UpdateModel(p);
                p.Shipping.MinValue = 0;// Utils.ParseDouble(Request["txtMinValue"]);
                p.Shipping.MaxValue = 0;// Utils.ParseDouble(Request["txtMaxValue"]);
                p.Shipping.ShipValue = Utils.ParseDouble(Request["txtShipValue"]);
                p.Shipping.AddDate = DateTime.Now;
                p.Shipping.AddBy = UserManager.GetUserId();
                p.Shipping.ModDate = DateTime.Now;               
                p.Shipping.ModBy = UserManager.GetUserId();
                eDataContext.Shippings.InsertOnSubmit(p.Shipping);
                eDataContext.SubmitChanges();
                #region Loai san pham
                //var listId = new List<int>();
                //var arrCatID = Request["chkCategory"].Split(',');
                //foreach (string key in arrCatID)
                //{
                //    if (key.ToLower() != "false")
                //    {
                //        int strId = Utils.ParseInt(key);
                //        if (strId > 0)
                //        {
                //            listId.Add(strId);
                //        }
                //    }
                //}
                //if (listId.Count > 0)
                //{
                //    foreach (var id in listId)
                //    {
                //        ShippingCategoryMap map = new ShippingCategoryMap();
                //        map.ShipID = p.Shipping.ID;
                //        map.CatID = id;
                //        eDataContext.ShippingCategoryMaps.InsertOnSubmit(map);
                //    }
                //    eDataContext.SubmitChanges();
                //}
                #endregion
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["MessageInfo"] = ex.Message;
                return View();
            }
        }
        #endregion

        #region Edit Price
        public ActionResult Edit(int id)
        {
            try
            {
                if (id < 0) throw new NotSupportedException();
                var c = eDataContext.Shippings.Where(ca => ca.ID == id).SingleOrDefault();
                return View(new ShippingEditViewModels
                {
                    Shipping = c,
                    ProductCategories = eDataContext.ProductCategories.OrderBy(e => e.Name).ToList()
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        [HttpPost]
        public ActionResult Edit(int id, ShippingEditViewModels p)
        {
            try
            {
                if (id < 0) throw new NotSupportedException();
                p.Shipping = eDataContext.Shippings.Where(e => e.ID == id).SingleOrDefault();
                UpdateModel(p);
                p.Shipping.MinValue = 0;// Utils.ParseDouble(Request["txtMinValue"]);
                p.Shipping.MaxValue = 0;// Utils.ParseDouble(Request["txtMaxValue"]);
                p.Shipping.ShipValue = Utils.ParseDouble(Request["txtShipValue"]);
                p.Shipping.ModDate = DateTime.Now;
                p.Shipping.ModBy = UserManager.GetUserId();
                eDataContext.SubmitChanges();
                #region Loai san pham
                //eDataContext.ShippingCategoryMaps.DeleteAllOnSubmit(p.Shipping.ShippingCategoryMaps);
                //eDataContext.SubmitChanges();
                //var listId = new List<int>();
                //var arrCatID = Request["chkCategory"].Split(',');
                //foreach (string key in arrCatID)
                //{
                //    if (key.ToLower() != "false")
                //    {
                //        int strId = Utils.ParseInt(key);
                //        if (strId > 0)
                //        {
                //            listId.Add(strId);
                //        }
                //    }
                //}
                //if (listId.Count > 0)
                //{
                //    foreach (var cid in listId)
                //    {
                //        ShippingCategoryMap map = new ShippingCategoryMap();
                //        map.ShipID = p.Shipping.ID;
                //        map.CatID = cid;
                //        eDataContext.ShippingCategoryMaps.InsertOnSubmit(map);
                //    }
                //    eDataContext.SubmitChanges();
                //}
                #endregion
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["MessageInfo"] = "Dữ liệu bị lỗi bạn hãy xem lại dữ liệu đầu vào";
                return View();
            }
        }

        #endregion

        #region Delete Price
        public ActionResult Delete(int id)
        {
            try
            {
                if (id < 0) throw new NotSupportedException();
                var c = eDataContext.Shippings.Where(ca => ca.ID == id).SingleOrDefault();
                return View(c);
            }
            catch (Exception ex)
            {
                ViewData["MessageInfo"] = "Có lỗi xảy ra bạn không thể xóa được";
                return View();
            }
        }
        [HttpPost]
        public ActionResult Delete(int id, FormCollection frm)
        {
            try
            {
                if (id < 0) throw new NotSupportedException();
                var c = eDataContext.Shippings.Where(ca => ca.ID == id).SingleOrDefault();
                eDataContext.ShippingCategoryMaps.DeleteAllOnSubmit(c.ShippingCategoryMaps);
                eDataContext.Shippings.DeleteOnSubmit(c);
                eDataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["MessageInfo"] = "Có lỗi xảy ra bạn không thể xóa được";
                return View();
            }
        }
        #endregion

        #region Detail
        public ActionResult Details(int id)
        {
            try
            {
                if (id < 0) throw new NotSupportedException();
                var s = eDataContext.Shippings.Where(ca => ca.ID == id).SingleOrDefault();
                return View(new ShippingEditViewModels
                {
                   Shipping=s
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
